package com.huang.weic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.weic.bean.imges;
import com.huang.weic.service.ImagesService;
import com.huang.weic.service.OssService;
import com.huang.weic.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/oss")
public class OssController {
    @Autowired
    private OssService service;
    @Autowired
    private ImagesService imagesService;
    @PostMapping("/upload/{viewId}")
    public R insertImages(MultipartFile file, @PathVariable Integer viewId){
        String s = service.uploadFileAvatar(file);
        QueryWrapper<imges> wrapper = new QueryWrapper<>();
        wrapper.eq("view_id",viewId);
        imges one = imagesService.getOne(wrapper);
        if(one==null){
            imges images = new imges();
            images.setImgList(s);
            images.setViewId(viewId);
            imagesService.save(images);
            return R.ok();
        }else{
            String list = one.getImgList();
             list=list+";"+s;
            one.setImgList(list);
            imagesService.updateById(one);
            return R.ok();
        }


    }
}
